package com.example.nonggo.api;

import android.util.Log;




import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.Buffer;

public class RequestLogInterceptor implements Interceptor {
    @Override
    public Response intercept(Interceptor.Chain chain) throws IOException {
        Request request = chain.request();
        // 捕获请求参数
        String method = request.method();
        String url = request.url().toString();
        String headers = request.headers().toString();
        String body = getRequestBody(request.body());  // 解析请求体

        Log.e("RequestLog", String.format(
                "Request:\nMethod: %s\nURL: %s\nHeaders: %s\nBody: %s",
                method, url, headers, body
        ));

        return chain.proceed(request);
    }

    private String getRequestBody(RequestBody body) throws IOException {
        if (body == null) return "null";
        Buffer buffer = new Buffer();
        body.writeTo(buffer);
        return buffer.readUtf8();  // 默认UTF-8编码
    }
}
